<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box {
            position: relative;
            width: 400px;
            border-bottom: 1px solid #ccc;
            margin: 100px auto;
        }
        
        .box input {
            width: 370px;
            height: 30px;
            border: 0;
            outline: none;
        }
        
        .box img {
            position: absolute;
            top: -4px;
            right: 10px;
            width: 24px;
        }
    </style>
</head>

<body>
    <!-- 思路：一个按钮2个状态，点击一次切换为文本，再点一次切换为密码框 -->
    <!-- 算法：利用一个变量，来判断flag值，1为文本框flag设置为0，0为密码框，flag设置为1 -->
    <div class="box">
        <label for="">
            <img src="../images/close.png" alt="" id="eye">
        </label>
        <input type="password" name="" id="pwd">

    </div>
    <script>
        var eye = document.getElementById('eye')
        var pwd = document.getElementById('pwd')
        var flag = 1;
        eye.onclick = function() {
            if (flag == 1) {
                pwd.type = "text";
                eye.src = "../images/open.png"
                flag = 0
            } else {
                pwd.type = "password";
                eye.src = "../images/close.png"
                flag = 1
            }

        }
    </script>
</body>

</html>